uniform sampler2D accumulationTexture,mappingTexture;
uniform float massScaleFactor;

void main()
{
	float zFactor = 0.2;
	vec4 accumulation;
	float stepValue;
	float clampedValue;
	float h0;
	float height;
	float dx = 0.0;
	float dy = 0.0;
	accumulation = texture2D(accumulationTexture,gl_TexCoord[0].st);
	h0 =  accumulation.r * massScaleFactor;
	stepValue = step(0.07,h0);
	clampedValue = stepValue * h0;
	clampedValue = min(clampedValue,1.3);
	
	accumulation = texture2D(accumulationTexture,gl_TexCoord[0].st + vec2(1.0/512.0,0));
	height = accumulation.r * massScaleFactor;
	dx += height;
	
	accumulation = texture2D(accumulationTexture,gl_TexCoord[0].st - vec2(1.0/512.0,0));
	height = accumulation.r * massScaleFactor;
	dx -= height;

	accumulation = texture2D(accumulationTexture,gl_TexCoord[0].st + vec2(0,1.0/512.0));
	height = accumulation.r * massScaleFactor;
	dy += height;
	
	accumulation = texture2D(accumulationTexture,gl_TexCoord[0].st - vec2(0,1.0/512.0));
	height = accumulation.r * massScaleFactor;
	dy -= height;
	
	vec3 n = vec3(dx ,dy ,zFactor);
	n = normalize(n);
	float l = dot(n,vec3(-0.8,0.4,0.5)) - 0.5;
	l = l*6;
	l = max(l,-0.0);
	
	vec2 displacementFromCenter = gl_TexCoord[0].st - vec2(0.5,0.5);
	displacementFromCenter *= 1.5;
	displacementFromCenter = (displacementFromCenter*0.0 + displacementFromCenter * (1.0f - clampedValue))/1.0;
	vec4 color = texture2D(mappingTexture,displacementFromCenter + vec2(0.28,0.42));
	
//	gl_FragColor = vec4(color.r  * clampedValue,color.g  * clampedValue ,color.b * clampedValue ,color.a * stepValue) + vec4(l,l,l,0) * stepValue;
	gl_FragColor = vec4(1,0,0,0) * clampedValue  + vec4(0,0,0,1) + vec4(0,l,l,0) * stepValue;
}
